package com.hf.eagle.web.struts;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hf.eagle.domain.Accessory;
import com.hf.eagle.domain.NewsInfo;
import com.hf.eagle.domain.ProjectExample;
import com.hf.eagle.domain.SaleGrid;
import com.hf.eagle.domain.SysDictionary;
import com.hf.eagle.domain.UserInfo;
import com.hf.eagle.web.base.BaseAction;
import com.hf.eagle.web.base.Pager;
import com.hf.eagle.web.base.SysConstant;

/**
 * 公司介绍
 * 
 * @author Xu,XiaoYuan
 * @version 2012.6.22 22:09
 */
public class CompanyIntroduceAction extends BaseAction {

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return this.list(mapping, form, request, response);
	}

	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean lazyForm = (DynaBean) form;
		String mod_id = (String) lazyForm.get("mod_id");

		request.setAttribute("mod_id", mod_id);

		SysDictionary sd = new SysDictionary();
		sd.setDictionary_id(SysConstant.PROJECT_EXAMPLE_TYPE);
		sd.setIs_del(0);
		List<SysDictionary> peTypeList = super.getFacade().getSysDictionaryService().getSysDictionaryList(sd);
		request.setAttribute("peTypeList", peTypeList);

		return mapping.findForward("list");
	}

	public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean lazyForm = (DynaBean) form;
		String mod_id = (String) lazyForm.get("mod_id");
		String type = (String) lazyForm.get("type");

		if (StringUtils.isNotBlank(mod_id)) {
			switch (Integer.valueOf(mod_id)) {
			case 2100: // 公司简介
				this.getNewsInfo(mod_id, request);
				break;
			case 2200: // 品牌介绍
				this.getNewsInfo(mod_id, request);
				break;
			case 2300: // 品牌荣誉
				this.getBrandHonors(mod_id, request);
				break;
			case 2400: // 销售网点
				this.getSaleGrids(mod_id, request, form);
				break;
			case 2500: // 公司愿景
				this.getNewsInfo(mod_id, request);
				break;
			case 2600: // 员工风采，默认显示员工感言
				mod_id = "2620";
				this.getStaffWords(mod_id, request, form);
				break;
			case 2620: // 员工感言
				this.getStaffWords(mod_id, request, form);
				break;
			case 2630: // 员工活动
				this.getStaffShow(mod_id, request, form);
				break;
			case 2700: // 工程案例
				this.getProjectExample(mod_id, type, request, form);
				break;
			default:
				break;
			}
		}

		request.setAttribute("mod_id", mod_id);

		return mapping.findForward("view");
	}

	public ActionForward viewGrid(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean dynaBean = (DynaBean) form;
		String grid_id = (String) dynaBean.get("grid_id");

		SaleGrid saleGrid = new SaleGrid();
		saleGrid.setGrid_id(Long.valueOf(grid_id));
		saleGrid.setIs_del(0);
		saleGrid = super.getFacade().getSaleGridService().getSaleGrid(saleGrid);

		super.copyProperties(form, saleGrid);

		return new ActionForward("/hf/CompanyIntroduce/viewGrid.jsp");
	}

	public ActionForward viewPE(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean dynaBean = (DynaBean) form;
		String project_id = (String) dynaBean.get("project_id");

		ProjectExample pe = new ProjectExample();
		pe.setProject_id(Long.valueOf(project_id));
		pe.setIs_del(0);
		pe = super.getFacade().getProjectExampleService().getProjectExample(pe);

		super.copyProperties(form, pe);

		return new ActionForward("/hf/CompanyIntroduce/viewPE.jsp");
	}

	private void getNewsInfo(String mod_id, HttpServletRequest request) {
		NewsInfo newsInfo = new NewsInfo();
		newsInfo.setMod_id(Long.valueOf(mod_id));
		newsInfo.setIs_del(0);
		newsInfo.setInfo_state(SysConstant.PUBLISH);
		newsInfo = getFacade().getNewsInfoService().getNewsInfo(newsInfo);

		if ("2100".equals(mod_id) || "2200".equals(mod_id) || "2500".equals(mod_id)) {// 更新浏览次数(公司简介，品牌介绍，公司愿景)
			if (null != newsInfo) {
				int view_count = newsInfo.getView_count();

				NewsInfo news_info = new NewsInfo();
				news_info.getMap().put("mod_id", mod_id);
				news_info.setMod_id(Long.valueOf(mod_id));
				news_info.setView_count(++view_count);
				super.getFacade().getNewsInfoService().modifyNewsInfo(news_info);

				newsInfo.setView_count(view_count);
			}
		}

		request.setAttribute("newsInfo", newsInfo);
	}

	private void getBrandHonors(String mod_id, HttpServletRequest request) {
		NewsInfo newsInfo = new NewsInfo();
		newsInfo.setMod_id(Long.valueOf(mod_id));
		newsInfo.setIs_del(0);
		newsInfo.setInfo_state(SysConstant.PUBLISH);
		List<NewsInfo> brand_honors = super.getFacade().getNewsInfoService().getNewsInfoList(newsInfo);
		if (null != brand_honors && brand_honors.size() > 0) {
			for (NewsInfo newsInfo2 : brand_honors) {
				List<Accessory> accessoryList = new ArrayList<Accessory>();
				Accessory acce_main = new Accessory();
				acce_main.setFile_level(SysConstant.MAIN_IMG);
				acce_main.getMap().put("mod_id", mod_id);
				acce_main.getMap().put("record_id", newsInfo2.getId());
				acce_main = super.getFacade().getAccessoryService().getAccessory(acce_main);
				accessoryList.add(acce_main);

				Accessory acce_sub = new Accessory();
				acce_sub.setFile_level(SysConstant.SUB_IMG);
				acce_sub.getMap().put("mod_id", mod_id);
				acce_sub.getMap().put("record_id", newsInfo2.getId());
				acce_sub = super.getFacade().getAccessoryService().getAccessory(acce_sub);
				accessoryList.add(acce_sub);
				newsInfo2.setAccessoryList(accessoryList);
			}
		}

		request.setAttribute("brand_honors", brand_honors);
	}

	private void getSaleGrids(String mod_id, HttpServletRequest request, ActionForm form) {
		DynaBean lazyForm = (DynaBean) form;
		Pager pager = (Pager) lazyForm.get("pager");

		SaleGrid sg = new SaleGrid();
		sg.setIs_del(0);
		super.copyProperties(sg, form);

		Long recordCount = super.getFacade().getSaleGridService().getSaleGridCount(sg);
		pager.init(recordCount, 8, pager.getRequestPage());
		sg.getRow().setFirst(pager.getFirstRow());
		sg.getRow().setCount(pager.getLastRow());

		List<SaleGrid> saleGrids = getFacade().getSaleGridService().getSaleGridPaginatedList(sg);
		if (null != saleGrids && saleGrids.size() > 0) {
			for (SaleGrid saleGrid : saleGrids) {
				Accessory acce = new Accessory();
				acce.setFile_level(SysConstant.SUB_IMG);
				acce.getMap().put("mod_id", mod_id);
				acce.getMap().put("record_id", saleGrid.getGrid_id());
				acce = super.getFacade().getAccessoryService().getAccessory(acce);
				saleGrid.setAccessory(acce);
			}
		}

		request.setAttribute("saleGrids", saleGrids);
	}

	private void getStaffWords(String mod_id, HttpServletRequest request, ActionForm form) {
		DynaBean lazyForm = (DynaBean) form;
		Pager pager = (Pager) lazyForm.get("pager");

		UserInfo userInfo = new UserInfo();
		userInfo.setIs_sys_user(0);
		userInfo.setIs_del(0);
		super.copyProperties(userInfo, form);

		Long recordCount = super.getFacade().getUserInfoService().getUserInfoCount(userInfo);
		pager.init(recordCount, 8, pager.getRequestPage());
		userInfo.getRow().setFirst(pager.getFirstRow());
		userInfo.getRow().setCount(pager.getLastRow());

		List<UserInfo> userInfos = getFacade().getUserInfoService().getUserInfoPaginatedList(userInfo);
		if (null != userInfos && userInfos.size() > 0) {
			for (UserInfo userInfo2 : userInfos) {
				Accessory acce = new Accessory();
				acce.setFile_level(SysConstant.SUB_IMG);
				acce.getMap().put("mod_id", "2610");
				acce.getMap().put("record_id", userInfo2.getUser_id());
				acce = super.getFacade().getAccessoryService().getAccessory(acce);
				if (StringUtils.isNotBlank(userInfo2.getStaff_word()) && userInfo2.getStaff_word().length() > 56)
					userInfo2.setStaff_word(userInfo2.getStaff_word().substring(0, 55));
				userInfo2.setAccessory(acce);
			}
		}

		request.setAttribute("userInfos", userInfos);
		request.setAttribute("flag", 1);
	}

	private void getStaffShow(String mod_id, HttpServletRequest request, ActionForm form) {
		DynaBean lazyForm = (DynaBean) form;
		Pager pager = (Pager) lazyForm.get("pager");

		NewsInfo newsInfo = new NewsInfo();
		newsInfo.setIs_del(0);
		newsInfo.setInfo_state(SysConstant.PUBLISH);
		super.copyProperties(newsInfo, form);
		newsInfo.setMod_id(Long.valueOf(mod_id));

		Long recordCount = super.getFacade().getNewsInfoService().getNewsInfoCount(newsInfo);
		pager.init(recordCount, 8, pager.getRequestPage());
		newsInfo.getRow().setFirst(pager.getFirstRow());
		newsInfo.getRow().setCount(pager.getLastRow());

		List<NewsInfo> newsInfos = getFacade().getNewsInfoService().getNewsInfoPaginatedList(newsInfo);
		request.setAttribute("newsInfos", newsInfos);
		request.setAttribute("flag", 2);
	}

	private void getProjectExample(String mod_id, String type, HttpServletRequest request, ActionForm form) {
		DynaBean lazyForm = (DynaBean) form;
		Pager pager = (Pager) lazyForm.get("pager");

		if (StringUtils.isBlank(type)) {
			type = "1";
		}

		ProjectExample pj = new ProjectExample();
		pj.setIs_del(0);
		pj.setProject_type(Integer.valueOf(type));
		super.copyProperties(pj, form);

		Long recordCount = super.getFacade().getProjectExampleService().getProjectExampleCount(pj);
		pager.init(recordCount, 9, pager.getRequestPage());
		pj.getRow().setFirst(pager.getFirstRow());
		pj.getRow().setCount(pager.getLastRow());

		List<ProjectExample> peList = getFacade().getProjectExampleService().getProjectExamplePaginatedList(pj);
		if (null != peList && peList.size() > 0) {
			for (ProjectExample pe : peList) {
				Accessory acce = new Accessory();
				acce.setFile_level(SysConstant.SUB_IMG);
				acce.getMap().put("mod_id", mod_id);
				acce.getMap().put("record_id", pe.getProject_id());
				acce = super.getFacade().getAccessoryService().getAccessory(acce);
				pe.setAccessory(acce);
			}
		}
		request.setAttribute("peList", peList);
	}

}